<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/19
 * Time: 14:26
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use app\common\model\Goods as GoodsModel;


class Goods extends AdminBase
{
    protected $goods_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->goods_model = new GoodsModel();
    }

    /**
     * 商品首页
     * @param int $page
     * @param string $keys
     * @return mixed
     */
    public function index($page = 1, $keys ='')
    {
        $map = [];

        if(!empty($keys)){
            $map['name'] = ['like', "%{$keys}%"];
        }

        $goods_list = Db::table('lsj_goods')
            ->where($map)
            ->order('id','desc')
            ->paginate(15,false,['page' => $page]);


        return $this->fetch('index',['goods_list' => $goods_list]);
    }

    /**
     * 添加商品页面
     * @return mixed
     */
    public function add()
    {
        if($this->request->isPost()){
            $data = $this->request->param();
            $validate_result = $this->validate($data,'Goods');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['created'] = date("Y-m-d H:i:s",time());
                if(isset($data['photos']) && is_array($data['photos'])){
                    $data['photos'] = json_encode($data['photos']);
                }
                if ($this->goods_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }

        }else{
            $label_list = Db::table('lsj_label')->select();
            return $this->fetch('add',['label_list'=>$label_list]);
        }
    }

    /**
     * 修改商品
     */
    public function edit($id)
    {
        if($this->request->isPost()){

            $data = $this->request->param();
            $validate_result = $this->validate($data,'Goods');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['created'] = date("Y-m-d H:i:s",time());
                if(is_array($data['photos'])){
                    $data['photos'] = json_encode($data['photos']);
                }
                if(!empty($data['label_ids'])){
                    $data['label_ids'] = implode("|",$data['label_ids'])."|";
                }
                if ($this->goods_model->allowField(true)->save($data,$id)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }else{
            $goods_list = Db::table('lsj_goods')->where("id={$id}")->find();
            if(!empty($goods_list['photos'])){
                $photos_array = json_decode($goods_list['photos']);
                $goods_list['photos'] = $photos_array;
            }



            $label_list = Db::table('lsj_label')->select();
            if(!empty($goods_list['label_ids'])){
                //判断多选框是否选中
                foreach ($label_list as $key=>$val){
                    if(strpos($goods_list['label_ids'],$val['id']."|") !== false){
                        $label_list[$key]['is_select'] = 1;
                    }else{
                        $label_list[$key]['is_select'] = 0;
                    }
                }
            }

            return $this->fetch('',['goods_list' => $goods_list,'label_list'=>$label_list]);
        }
    }

    /**
     * 更改状态
     */
    public function changeStatus($id = [],$type = '')
    {
        if(!empty($id)){
            switch($type){
                case 'up':
                    //上架状态
                    $key = 'is_up';
                    $val = 1;
                    break;
                case 'down':
                    //下架状态
                    $key = 'is_up';
                    $val = 0;
                    break;
                case 'recommend':
                    //推荐
                    $key = 'is_recommend';
                    $val = 1;
                    break;
                case 'no_recommend':
                    //取消推荐
                    $key = 'is_recommend';
                    $val = 0;
                    break;
            }

            foreach($id as $goods_id){
                $data[] = ['id' => $goods_id, "$key" => $val];
            }
            if ($this->goods_model->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }else{
            $this->error('请选择需要操作的商品');
        }

    }
}